package com.yy.permissions_general.module.system.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yy.permissions_general.common.config.exception.base.ServiceException;
import com.yy.permissions_general.common.constant.StatusEnum;
import com.yy.permissions_general.module.system.dict.entity.DictData;
import com.yy.permissions_general.module.system.dict.mapper.DictDataMapper;
import com.yy.permissions_general.module.system.dict.service.DictDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典详情 服务实现类
 * </p>
 *
 * @author YY
 * @since 2021-08-08
 */
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements DictDataService {

    @Resource
    private  DictDataMapper dictDataMapper;
    /**
    * 分页查询列表
    * @param dictData
    * @return
    */
    @Override
    public  Page<DictData>selectPageList(DictData dictData, int limit, int page)  {
        Page pageContext = new Page(page,limit);
        Page<DictData> pageList = this.baseMapper.selectPage(pageContext, dictData);
        return pageList;
    }

    @Override
    public Page<Object> getDictDataXMSelect(Map<String, Object> map, int limit, int page) {
        Page pageContext = new Page(page,limit);
        Page<Object> pageList = dictDataMapper.getDictDataXMSelect(pageContext, map);
        return pageList;
    }

    /**
    * 新增数据
    * @param dictData
    */
    @Override
    public int addDictData(DictData dictData) {
        dictData.setDictDataCode(dictData.getDictDataCode().trim());
        dictData.setDictDataName(dictData.getDictDataName().trim());
        int resultInt = checkDictData(dictData,false);
        if (resultInt == 1){
            throw new ServiceException(StatusEnum.REPEAT_ADD_ERROR);
        }
        return baseMapper.insert(dictData);
    }

    /**
    * 修改数据
    * @param dictData
    */
    @Override
    public int editDictData(DictData dictData) {
        dictData.setDictDataCode(dictData.getDictDataCode().trim());
        dictData.setDictDataName(dictData.getDictDataName().trim());
        int resultInt = checkDictData(dictData,true);
        if (resultInt == 1){
            throw new ServiceException(StatusEnum.REPEAT_ADD_ERROR);
        }
        return baseMapper.updateById(dictData);
    }
    /**
     * 判断是否重复添加
     * @param dictData
     * @param isUpdate
     * @return
     */
    private int checkDictData(DictData dictData, boolean isUpdate) {
        int resultInt = 0;
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<DictData>();
        if (isUpdate){
            queryWrapper.ne("id",dictData.getId());
        }
        queryWrapper.eq("dict_id",dictData.getDictId());
        queryWrapper.eq("dict_data_code",dictData.getDictDataCode());
        DictData checkDict = this.getOne(queryWrapper);
        if (checkDict != null){
            resultInt = 1;
            return resultInt;
        }
        queryWrapper = new QueryWrapper<DictData>();
        if (isUpdate){
            queryWrapper.ne("id",dictData.getId());
        }
        queryWrapper.eq("dict_id",dictData.getDictId());
        queryWrapper.eq("dict_data_name",dictData.getDictDataName());
        checkDict = this.getOne(queryWrapper);
        if (checkDict != null){
            resultInt = 1;
        }
        return resultInt;
    }
    /**
    * 查看详情
    * @param id
    * @return
    */
    @Override
    public DictData getDetailsById(Integer id) {
        return baseMapper.selectById(id);
    }

    /**
    * 删除数据
    * @param id
    * @return
    */
    @Override
    public int deleteById(Integer id) {
        return baseMapper.deleteById(id);
    }

    /**
    * 批量删除
    * @param ids
    * @return
    */
    @Override
    public int deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids);
    }

    /**
     * 删除字典详情
     * @param dictId
     * @return
     */
    @Override
    public int deleteByDictId(Integer dictId) {
        return baseMapper.deleteByDictId(dictId);
    }

    /**
     * 修改状态
     * @param dictData
     * @return
     */
    @Override
    public int updateDictData(DictData dictData) {
        return baseMapper.updateById(dictData);
    }

}

